/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package cryptoprovider

var cryptoPlugin *CryptoProvider

type CryptoInterface interface {
	Encrypt(plain string) string
	Check(originCipher, attempCipher string) bool
	Decrypt(cipher string) (string, error)
}

// Authen 密码接口
type CryptoProvider struct {
	provider CryptoInterface
}

// 初始化认证插件
func Init(cryptoType string) *CryptoProvider {
	cryptoPlugin = new(CryptoProvider)
	switch cryptoType {
	case "bcrypt":
		cryptoPlugin.provider = NewBcrypt(nil, nil)
		break
	case "sha256withsalt":
		cryptoPlugin.provider = NewSHA2(nil)
		break
	default:
		cryptoPlugin.provider = NewSHA2(nil)
		break
	}
	return cryptoPlugin
}

func Get() *CryptoProvider {
	return cryptoPlugin
}

func (ap *CryptoProvider) Encrypt(plain string) string {
	return ap.provider.Encrypt(plain)
}
func (ap *CryptoProvider) Check(originCipher, attempCipher string) bool {
	return ap.provider.Check(originCipher, attempCipher)
}
func (ap *CryptoProvider) Decrypt(cipher string) (string, error) {
	return ap.provider.Decrypt(cipher)
}
